package lesson13;

import java.io.*;
import java.sql.SQLOutput;
import java.util.Scanner;

/**
 * @author tongchen
 * @create 2023-02-12 23:22
 */
public class ExerAndTest2 {
    public static void main(String[] args) {
        Scanner scan=new Scanner(System.in);
        System.out.println("请输入你要复制的文件");
        String fileName=scan.nextLine();
        //检验文件的合法性
        File file = new File(fileName);
        if(!file.exists()){
            System.out.println("文件不存在！");
            return;
        }
        //输入复制的目录
        System.out.println("请输入你要复制的目的地");
        String dest=scan.nextLine();
        File file1 = new File(dest);
        //判断父目录
        if(!file1.getParentFile().exists()){
            System.out.println("父目录不存在");
            return;
        }
        if (file1.exists()){
            //判断文件是否存在
            System.out.println("文件已存在");
            return;

        }
        //创建输入输出流
        try (InputStream inputStream=new FileInputStream(fileName);OutputStream outputStream=new FileOutputStream(dest)){
            //创建输入流
            Scanner scanner = new Scanner(inputStream,"UTF-8");
            PrintStream printStream= new PrintStream(outputStream);
            //创建数组
            byte[]bytes=new byte[1024];
            while(scanner.hasNextLine()){
                String s=scanner.nextLine();
                System.out.println(s);
                printStream.println(s);
            }
            System.out.println("复制成功");
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }
}
